├── .gitignore ├── lib ├── tests │ └── index.js └── core │ └── Dispatcher.js ├── README.md ├── package.json ├── .vscode └── launch.json └── example ├── index.js └── models └── Account.js /.gitignore: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /lib/tests/index.js: -------------------------------------------------------------------------------- 1 | // Someday... 2 | throw new Error('Not Implemented') 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Message Passing 2 | 3 | Пример реализации посылки сообщений на JS 4 | 5 | Пример адаптирован из книги - ["Структура и интерпретация компьютерных программ"](https://www.ozon.ru/context/detail/id/5322055/?partner=ExtremeCode&utm_content=link) 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "message-passing", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "node ./example/index.js" 8 | }, 9 | "keywords": ["js", "oop", "message-passing", "functional"], 10 | "author": "Artem Dontsov", 11 | "license": "MIT" 12 | } 13 | -------------------------------------------------------------------------------- /lib/core/Dispatcher.js: -------------------------------------------------------------------------------- 1 | 2 | const Dispatcher = (methods) => function (method) { 3 | const def = () => false 4 | 5 | let args = Array.from(arguments) 6 | if (methods === undefined) { 7 | throw new Error('Methods table not initialized') 8 | } 9 | 10 | if (typeof(method) === 'string') { 11 | args = args.splice(1, args.length) 12 | const handler = methods[method] || def 13 | return handler(...args) 14 | } 15 | 16 | return () => { } 17 | } 18 | 19 | module.exports = Dispatcher 20 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "node", 9 | "request": "launch", 10 | "name": "Launch Program", 11 | "program": "${workspaceFolder}\\example\\index.js", 12 | "outputCapture": "std" 13 | } 14 | ] 15 | } -------------------------------------------------------------------------------- /example/index.js: -------------------------------------------------------------------------------- 1 | const Account = require('./models/Account') 2 | 3 | const main = () => { 4 | let name 5 | let balance 6 | let account = Account(100) 7 | 8 | balance = account('withdraw', 10) 9 | balance = account('withdraw', 20) 10 | balance = account('withdraw', 20) 11 | 12 | account('setFirstName', 'Extreme') 13 | account('setLastName', 'Code') 14 | 15 | account('notExistedMethod') 16 | 17 | name = account('getName') 18 | 19 | console.log('Name: ') 20 | console.log(name) 21 | 22 | console.log('Balance: ') 23 | console.log(balance) 24 | } 25 | 26 | main() 27 | -------------------------------------------------------------------------------- /example/models/Account.js: -------------------------------------------------------------------------------- 1 | const Dispatcher = require('../../lib/core/Dispatcher') 2 | 3 | const Account = (amount) => { 4 | let balance = amount 5 | let firstName = null 6 | let lastName = null 7 | 8 | const methods = { 9 | withdraw: (count) => { 10 | if (count > balance) { 11 | throw new Error('Not enough cash') 12 | } 13 | 14 | balance -= count 15 | return balance 16 | }, 17 | deposit: (count) => { 18 | balance += count 19 | return balance 20 | }, 21 | setFirstName: (name) => { 22 | firstName = name 23 | return firstName 24 | }, 25 | setLastName: (name) => { 26 | lastName = name 27 | return lastName 28 | }, 29 | getName: () => { 30 | return `${firstName} ${lastName}` 31 | } 32 | } 33 | 34 | return Dispatcher(methods) 35 | } 36 | 37 | module.exports = Account 38 | --------------------------------------------------------------------------------