Skip to content
This repository has been archived by the owner on Feb 14, 2023. It is now read-only.

Commit

Permalink
resolves #29
Browse files Browse the repository at this point in the history
  • Loading branch information
quisido committed Feb 1, 2019
1 parent 7c6f293 commit 89fe07f
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 3 deletions.
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ declare class GlobalPureComponent<P = {}, S = {}> extends React.PureComponent<P,
setGlobal(newGlobal: NewGlobal, callback?: GlobalCallback): Promise<void> | void;
}

type GlobalCallback = (state: GlobalState) => NewGlobal;

type GlobalCallbackRemover = () => void;

type GlobalReducer = (state: GlobalState, ...args: any[]) => NewGlobal;

type GlobalPropertySetter<T> = (value: T) => void;
Expand All @@ -46,10 +50,12 @@ type NewGlobalFunction = (global: GlobalState) => NewGlobal;

interface ReactN {
(Component: React.ComponentType): typeof GlobalComponent;
addCallback(callback: GlobalCallback): GlobalCallbackRemover;
addReducer(name: string, reducer: GlobalReducer): void;
Component: typeof GlobalComponent;
default: ReactN;
PureComponent: typeof GlobalPureComponent;
removeCallback(callback: GlobalCallback): void;
resetGlobal(): void;
setGlobal(newGlobal: NewGlobal, callback?: GlobalCallback): Promise<void> | void;
useGlobal(): [ GlobalState, GlobalStateSetter ];
Expand Down
26 changes: 24 additions & 2 deletions src/global-state-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ class GlobalStateManager {
this.reset();
}

addCallback(callback) {
this._callbacks.add(callback);
return () => {
this.removeCallback(callback);
};
}

// Map component instance to a state property.
addPropertyListener(property, propertyListener) {
if (this._propertyListeners.has(property)) {
Expand All @@ -17,7 +24,7 @@ class GlobalStateManager {
else {
this._propertyListeners.set(property, new Set([ propertyListener ]));
}
};
}

// Begin a transaction.
beginTransaction() {
Expand Down Expand Up @@ -49,9 +56,20 @@ class GlobalStateManager {
propertyListener();
}

// Call each global callback.
for (const callback of this._callbacks) {

// Delay these under after the current transaction has deleted?
this.setAny(callback(this.stateWithReducers));
}

this._transactions.delete(transactionId);
}

removeCallback(callback) {
this._callbacks.remove(callback);
}

// Unmap a component instance from all state properties.
removePropertyListener(propertyListener) {

Expand All @@ -68,6 +86,7 @@ class GlobalStateManager {

// Reset the global state.
reset() {
this._callbacks = new Set();
this._propertyListeners = new Map();
this._state = Object.create(null);
this._transactionId = 0;
Expand Down Expand Up @@ -106,7 +125,10 @@ class GlobalStateManager {
setAny(any) {

// No changes, e.g. getDerivedGlobalFromProps.
if (any === null) {
if (
any === null ||
typeof any === 'undefined'
) {
return;
}

Expand Down
5 changes: 5 additions & 0 deletions src/helpers/add-callback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const globalStateManager = require('../global-state-manager');

module.exports = function addCallback(f) {
return globalStateManager.addCallback(f);
};
5 changes: 5 additions & 0 deletions src/helpers/remove-callback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const globalStateManager = require('../global-state-manager');

module.exports = function removeCallback(f) {
return globalStateManager.removeCallback(f);
};
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ const React = require('react');
const { ReactNComponent, ReactNPureComponent } = require('./components');
const ReactN = require('./decorator');
const {
addReducer, getGlobal, resetGlobal, setGlobal, useGlobal, withGlobal
addCallback, addReducer, getGlobal, removeCallback, resetGlobal, setGlobal,
useGlobal, withGlobal
} = require('./helpers/index');

Object.assign(ReactN, React, {
addCallback,
addReducer,
Component: ReactNComponent,
default: ReactN,
getGlobal,
PureComponent: ReactNPureComponent,
removeCallback,
resetGlobal,
setGlobal,
useGlobal,
Expand Down
9 changes: 9 additions & 0 deletions tests/helpers/add-callback.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
describe('addCallback', () => {

it('should add a callback');

describe('the added callback', () => {
it('should execute on state change');
});

});
9 changes: 9 additions & 0 deletions tests/helpers/remove-callback.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
describe('removeCallback', () => {

it('should remove a callback');

describe('the removed callback', () => {
it('should not execute on state change');
});

});

0 comments on commit 89fe07f

Please sign in to comment.