Created
November 19, 2018 16:06
-
-
Save oneEyedSunday/67219c08a337240ebf63e46e15553c3e to your computer and use it in GitHub Desktop.
Demonstration of the basic OOP principles
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
const getData = limit => { | |
const data = []; | |
let index = 0; | |
while(index < limit){ | |
data.push(`Item ${index}`); | |
index++; | |
} | |
return data; | |
} | |
const DB = getData(2) | |
const identifier = Symbol('name'); | |
class Socket { | |
constructor(db){ | |
this.db = db; | |
this[identifier] = `Socket #${Date.now()}`; | |
console.log('Created socket: ', this[identifier]); | |
} | |
data() { return dataGenerator; }; | |
name() { return this[identifier]} | |
receive(item) { | |
console.log('Server Received Data'); | |
this.db.push(item); | |
} | |
} | |
class SocketAware { | |
constructor(source) { | |
console.log('Initiating Socket Aware object from source: ', source.name()); | |
this.source = source; | |
} | |
close() { | |
console.log('Connection closed'); | |
this.read = undefined; | |
} | |
read() { | |
console.log(this.source.data().next()); | |
} | |
} | |
class ReadWriter extends SocketAware { | |
constructor(source) { | |
super(source); | |
} | |
send(data) { | |
console.log(`Sending ${data} to source`); | |
if (typeof data === 'number') { | |
this.source.receive(`Numeric Item ${data}`) | |
} else { | |
this.source.receive(data); | |
} | |
} | |
} | |
class Reader extends SocketAware { | |
read() { | |
console.log('Reading from Dedicated Socket Reader') | |
super.read(); | |
} | |
} | |
function* dataFromSource(){ | |
yield* DB; | |
} | |
const dataGenerator = dataFromSource(); | |
const source = new Socket(DB); | |
const sck = new Reader(new Socket(DB)); | |
const rwSck = new ReadWriter(source); | |
sck.read(); | |
rwSck.send(`New data from RW client`); | |
rwSck.send(305); | |
rwSck.send({ | |
payload: [1,2,34] | |
}); | |
rwSck.read(); | |
rwSck.close(); | |
// rwSck.read(); | |
sck.read(); | |
sck.read(); | |
sck.read() | |
sck.read(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment