Skip to content

Commit

Permalink
feat: implements Ivy Hooks via Mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
thekiba committed Jan 20, 2019
1 parent ca9c15d commit 9c99fae
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions projects/platform/src/lib/ivy-hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './use-state.hook';
54 changes: 54 additions & 0 deletions projects/platform/src/lib/ivy-hooks/use-effect.hook.ts
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);
};
};
}
15 changes: 15 additions & 0 deletions projects/platform/src/lib/ivy-hooks/use-reducer.hook.ts
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;
};
};
}
14 changes: 14 additions & 0 deletions projects/platform/src/lib/ivy-hooks/use-state.hook.ts
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;
};
};
}
2 changes: 2 additions & 0 deletions projects/platform/src/lib/platform.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ export class NgxfModule {
};
}
}

export * from './ivy-hooks';

0 comments on commit 9c99fae

Please # to comment.