-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implements Ivy Hooks via Mixins
- Loading branch information
Showing
5 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './use-state.hook'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { SimpleChanges } from '@angular/core'; | ||
|
||
const noop = () => { }; | ||
export function UseEffect( | ||
effect: () => (() => any) | any, | ||
affectedKeys?: string[] | ||
) { | ||
const destroyEffectsMap = new WeakMap(); | ||
function init() { | ||
const destroyEffect = effect.call(this); | ||
destroyEffectsMap.set(this, destroyEffect); | ||
} | ||
function destroy() { | ||
if (destroyEffectsMap.has(this)) { | ||
const destroyEffect = destroyEffectsMap.get(this); | ||
if (destroyEffect) { | ||
destroyEffect(); | ||
} | ||
} | ||
} | ||
function isChanged(changes: SimpleChanges): boolean { | ||
if (!affectedKeys) { | ||
return true; | ||
} | ||
|
||
if (affectedKeys.length === 0) { | ||
return false; | ||
} | ||
|
||
return affectedKeys.every((affectedKey) => affectedKey in changes); | ||
} | ||
return (def: any) => { | ||
const originalOnInit = def.ngComponentDef.onInit || noop; | ||
def.ngComponentDef.onInit = function onInit() { | ||
originalOnInit(); | ||
init.call(this); | ||
}; | ||
|
||
const originalOnChanges = def.ngComponentDef.onChanges || noop; | ||
def.ngComponentDef.onChanges = function onChanges(changes: SimpleChanges) { | ||
originalOnChanges(changes); | ||
if (isChanged(changes)) { | ||
destroy.call(this); | ||
init.call(this); | ||
} | ||
}; | ||
|
||
const originalOnDestroy = def.ngComponentDef.onDestroy || noop; | ||
def.ngComponentDef.onDestroy = function onDestroy() { | ||
originalOnDestroy(); | ||
destroy.call(this); | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export function UseReducer<T = any>( | ||
[stateKey, dispatchKey]: [string, string], | ||
reducer: (state: T, action: { type: any }) => any, | ||
initialValue?: T | ||
) { | ||
return (def: any) => { | ||
const originalFactory = def.ngComponentDef.factory; | ||
def.ngComponentDef.factory = () => { | ||
const cmp = originalFactory(def.ngComponentDef.type); | ||
cmp[stateKey] = initialValue; | ||
cmp[dispatchKey] = (action) => cmp[stateKey] = reducer(cmp[stateKey], action); | ||
return cmp; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export function UseState<T = any>( | ||
[stateKey, setStateKey]: [string, string], | ||
initialValue?: T | ||
) { | ||
return (def: any) => { | ||
const originalFactory = def.ngComponentDef.factory; | ||
def.ngComponentDef.factory = () => { | ||
const cmp = originalFactory(def.ngComponentDef.type); | ||
cmp[stateKey] = initialValue; | ||
cmp[setStateKey] = (value) => cmp[stateKey] = value; | ||
return cmp; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,3 +57,5 @@ export class NgxfModule { | |
}; | ||
} | ||
} | ||
|
||
export * from './ivy-hooks'; |