-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use context changed bits to observe state slices in parts
- Loading branch information
Showing
3 changed files
with
157 additions
and
57 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 |
---|---|---|
@@ -1,5 +1,42 @@ | ||
import React from 'react' | ||
|
||
const Context = React.createContext(null) | ||
const INDEX_SIZE = 31-3; | ||
|
||
const allMarked = () => 0xFFFF << 2; | ||
|
||
export const createHashFunction = (state) => { | ||
if (Array.isArray(state)) { | ||
return allMarked; | ||
} | ||
const keys = {}; | ||
Object | ||
.keys(state) | ||
.forEach((key, index) => keys[key] = 1 << (3 + index % INDEX_SIZE)) | ||
|
||
return (hash, key) => hash | keys[key]; | ||
} | ||
|
||
const Context = React.createContext(null, (prev, next) => { | ||
let result = 1; // one bit always changed 1 to enable non pure connect | ||
|
||
if (typeof prev.state !== 'object' || Array.isArray(next.state)) { | ||
return 0xFFFFFF | ||
} | ||
// foo has been changed | ||
if (prev.store !== next.store) { | ||
result |= 2 | ||
} | ||
if (prev.hashFunction !== next.hashFunction) { | ||
result |= 4 | ||
} | ||
Object | ||
.keys(prev.state) | ||
.forEach(key => { | ||
if (prev.state[key] !== next.state[key]) { | ||
result = next.hashFunction(result, key) | ||
} | ||
}) | ||
return result | ||
}) | ||
|
||
export default Context |
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
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